### For new version of rdrobust, 2017:
rd.export <- function(rd.fits,file=FALSE,booktabs=TRUE){
      if(length(rd.fits)==1){
            rd.fit <- unlist(rd.fits[[1]])
            out.table.initial <- round(cbind(rd.fit$coef1,rd.fit$ci3,rd.fit$ci6,rd.fit$pv3,#(rd.fit$N1+rd.fit$N2),
            (rd.fit$Nh1+rd.fit$Nh2),(100*rd.fit$bws1)),3)
            out.table <- cbind(paste(rd.fit$call)[2],out.table.initial)
            out.table <- cbind(out.table[,1],out.table[,2],out.table[,5],out.table[,6],out.table[,7])
            out.table2 <- cbind("", paste("(",out.table.initial[,2],", ",out.table.initial[,3],")",sep=""),"","","")
            colnames(out.table) <- c("Outcome variable","Estimate","Pr >|z|","Eff. N","BW")
            thisoutput <- (out.table)
            thisoutput2 <- (out.table2)
            output <- rbind(thisoutput,thisoutput2)     
            }
      if(length(rd.fits)>1){
            output <- NULL
            for(i in 1:length(rd.fits)){
                  rd.fit <- unlist(rd.fits[i])
                  # summary(rd.fit)
                  out.table.initial <- round(cbind(rd.fit$coef1,rd.fit$ci3,rd.fit$ci6,rd.fit$pv3,#(rd.fit$N1+rd.fit$N2),
            (rd.fit$Nh1+rd.fit$Nh2),(100*rd.fit$bws1)),3)
                  out.table <- cbind(paste(rd.fit$call)[2],out.table.initial)
                  out.table <- cbind(out.table[,1],out.table[,2],out.table[,5],out.table[,6],out.table[,7])
                  out.table2 <- cbind("", paste("(",out.table.initial[,2],", ",out.table.initial[,3],")",sep=""),"","","")
                  colnames(out.table) <- c("Outcome variable","Estimate","Pr >|z|","Eff. N","BW")
                  colnames(out.table2) <- c("Outcome variable","(Robust CI)","Pr >|z|","Eff. N","BW")
                  thisoutput <- (out.table)
                  thisoutput2 <- (out.table2)
                  output <- (rbind(output,thisoutput,thisoutput2))
            }
            
      }
      if(booktabs==TRUE){
            if(file==TRUE){
                  print(xtable(output),
                        file=paste0(file),include.rownames=F,floating = FALSE,
                        hline.after=NULL, add.to.row=list(pos=list(-1,0,nrow(output)),
                                                          command=c('\\toprule\n',
                                                                    '\\midrule\n',
                                                                    '\\bottomrule\n'))) 
            }
            if(file==FALSE){
                  print(xtable(output)
                        ,include.rownames=F,
                        hline.after=NULL, add.to.row=list(pos=list(-1,0,nrow(output)),
                                                          command=c('\\toprule\n',
                                                                    '\\midrule\n',
                                                                    '\\bottomrule\n'))) 
            }
      }
      if(booktabs==FALSE){
            if(file==TRUE){
                  print(xtable(output),
                        file=paste0(file),include.rownames=F,floating = FALSE) 
            }
            if(file==FALSE){
                  print(xtable(output)
                        ,include.rownames=F) 
            }
      }
}